package com.example.testapp.activity.activity.menu;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.testapp.R;
import com.example.testapp.activity.util.CalendarContract;
import com.example.testapp.activity.view.BorderTextView;

/**
 * Created by l on 2016/6/10.
 */
public class CalendarTypeActivity extends Activity {

    private CalendarContract  cc = null;
    private LinearLayout  layout; // 布局 ， 可以在xml布局中获得
    private LinearLayout  layButton;
    private RadioGroup  group;
    private RadioButton button;
    private BorderTextView textTop = null;
    private BorderTextView textSave = null;
    private BorderTextView textCancel = null;
    private int sch_typeID = 0;
    private int remindID = 0;
    private int schType_temp = 0;
    private int remind_temp = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        cc = new CalendarContract();

        layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundResource(R.mipmap.schedule_bk);
        layout.setLayoutParams(params);

        group = new RadioGroup(this);
        textSave = new BorderTextView(this, null);
        textCancel=new BorderTextView(this, null);
        textTop = new BorderTextView(this, null);
        textTop.setTextColor(Color.BLACK);
        textTop.setBackgroundResource(R.mipmap.top_day);
        textTop.setText("日程类型");
        textTop.setHeight(100);
        textTop.setGravity(Gravity.CENTER);
        layout.addView(textTop);

        Intent intent = getIntent();
        int sch_remind[] = intent.getIntArrayExtra("sch_remind");
        if(sch_remind != null){
            sch_typeID = sch_remind[0];
            remindID = sch_remind[1];
        }
        for (int i = 0; i<cc.sch_type.length; i++){
            button = new RadioButton(this);
            if(i == sch_typeID){
                button.setChecked(true);
            }
            button.setText(cc.sch_type[i]);
            button.setId(i);
            button.setTextColor(Color.BLACK);
            group.addView(button);
        }
        layout.addView(group);

        layButton = new LinearLayout(this);
        layButton.setOrientation(LinearLayout.HORIZONTAL);
        layButton.setLayoutParams(params);
        textSave.setTextColor(Color.BLACK);
        textSave.setBackgroundResource(R.mipmap.top_day);
        textSave.setText("确定");
        textSave.setWidth(200);
        textSave.setHeight(80);
        textSave.setGravity(Gravity.CENTER);
        textSave.setClickable(true);
        textCancel.setTextColor(Color.BLACK);
        textCancel.setBackgroundResource(R.mipmap.top_day);
        textCancel.setText("取消");
        textCancel.setWidth(200);
        textCancel.setHeight(80);
        textCancel.setGravity(Gravity.CENTER);
        textCancel.setClickable(true);
        layButton.addView(textSave);
        layButton.addView(textCancel);
        layout.addView(layButton);
        this.setContentView(layout);

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                schType_temp = checkedId;
                new AlertDialog.Builder(CalendarTypeActivity.this)
                         .setTitle("日程类型")
                         .setIcon(android.R.drawable.ic_dialog_info)
                         .setSingleChoiceItems(
                             new String[] {cc.remind[0],cc.remind[1],cc.remind[2],cc.remind[3],cc.remind[4],cc.remind[5],cc.remind[6],cc.remind[7]}
                                 ,remindID,
                             new DialogInterface.OnClickListener(){
                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {
                                     remind_temp = which;
                                 }
                              }).setPositiveButton("确认", null).setNegativeButton("取消", null).show();
                    }
         });

        textSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sch_typeID = schType_temp;
                remindID = remind_temp;
                Intent  intent = new Intent();
                intent.setClass(CalendarTypeActivity.this, CalendarViewActivity.class);
                intent.putExtra("schType_remind", new int[]{sch_typeID, remindID});
                startActivity(intent);
            }
        });

        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent  intent = new Intent();
                intent.setClass(CalendarTypeActivity.this, CalendarViewActivity.class);
                intent.putExtra("schType_remind", new int[]{sch_typeID, remindID});
                startActivity(intent);
            }
        });
    }
}
